const fs = require('fs');
const path = require('path');

// 模拟路由中的逻辑来验证课程标题是否能正确获取
console.log('=== Testing Course Title Retrieval ===');

const coursesIndexFile = path.join(__dirname, '../resources/courses/index.json');

try {
  const indexContent = fs.readFileSync(coursesIndexFile, 'utf8');
  const coursesIndex = JSON.parse(indexContent);
  
  console.log('Courses Index:');
  console.log(coursesIndex);
  
  // 模拟数据库中的进度数据
  const mockProgressData = [
    { id: 1, user_id: 1, course_id: 1, is_completed: 0, completion_percentage: 2.08 },
    { id: 2, user_id: 1, course_id: 2, is_completed: 0, completion_percentage: 0 }
  ];
  
  console.log('\nMock Progress Data:');
  console.log(mockProgressData);
  
  // 将课程标题添加到进度数据中
  const enrichedRows = mockProgressData.map(row => {
    const course = coursesIndex.find(c => c.id === row.course_id);
    return {
      ...row,
      title: course ? course.title : `Course ${row.course_id}`
    };
  });
  
  console.log('\nEnriched Progress Data with Titles:');
  console.log(enrichedRows);
  
} catch (readError) {
  console.error('Error reading courses index:', readError);
}